package net.maku.wx.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.wx.convert.WxUserConvert;
import net.maku.wx.entity.WxUserEntity;
import net.maku.wx.service.WxUserService;
import net.maku.wx.query.WxUserQuery;
import net.maku.wx.vo.WxUserVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* 开发者
*
* @author Macro 
* @since 1.0.0 2022-09-01
*/
@RestController
@RequestMapping("wx/wxuser")
@Tag(name="开发者")
@AllArgsConstructor
public class WxUserController {
    private final WxUserService wxUserService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('wx:wxuser:page')")
    public Result<PageResult<WxUserVO>> page(@Valid WxUserQuery query){
        PageResult<WxUserVO> page = wxUserService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('wx:wxuser:info')")
    public Result<WxUserVO> get(@PathVariable("id") Long id){
        WxUserEntity entity = wxUserService.getById(id);

        return Result.ok(WxUserConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('wx:wxuser:save')")
    public Result<String> save(@RequestBody WxUserVO vo){
        wxUserService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('wx:wxuser:update')")
    public Result<String> update(@RequestBody @Valid WxUserVO vo){
        wxUserService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('wx:wxuser:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        wxUserService.delete(idList);

        return Result.ok();
    }
}